layui.extend({
    apiconfig: './public/apiconfig'
});
layui.use(['laypage', 'element', 'apiconfig','form'], function() {
    var laypage = layui.laypage,
        element = layui.element,
        apiconfig = layui.apiconfig,
        layer=layui.layer,
        form=layui.form,
        $ = layui.jquery;
        // 在头部存accessToken
        if (!(sessionStorage.getItem('accessToken')) && location.pathname != "./login.html") {
            sessionStorage.clear();
            localStorage.clear();
            location.href = "./login.html";
        }
        $(".layui-tab-content").height(window.screen.height-210);
        $("#userName").html(sessionStorage.getItem("userDept"));
        xadmin.is_remember = true;
        // 配置tab存储名称
        xadmin.data_name = 'tab_list_index';
        var loading;
        // 点击事件
        var active={
            editPass:function(){
                var index=layer.open({
                    type: 1
                    ,title:"修改密码"
                    ,shade:[0.5, '#000']
                    ,id: 'layerDemo1' //防止重复弹出
                    ,area:['345px','406px']
                    ,content: html
                    ,btn: ["确定","取消"]
                    ,btnAlign: 'c' //按钮居中
                    ,yes: function(){
                        form.on('submit(Editpassword)',function(d){
                            if(d.field.secondpass!=d.field.newpass){
                                layer.msg("确认密码与新密码不一致!",{icon:2,time:2000});
                                d.field.secondpass='';
                                d.field.newpass='';
                                return false;
                            }
                            var str={
                                "id": sessionStorage.getItem("userid"),
                                "newPassword":d.field.newpass,
                                "oldPassword":d.field.oldpass
                            }
                            $.ajax({
                                url: "gt/opm/user/password", //请求地址
                                type: "PUT", //提交的方式S
                                data:str, //提交的数据
                                dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                                beforeSend: function(request) {
                                    loading=layer.load(2);
                                    request.setRequestHeader("access-token",sessionStorage.getItem("accessToken"))
                                },
                                ContentType:"application/json;charset=UTF-8",
                                success: function(data) { 
                                    layer.closeAll('loading');
                                    if (data.flag == true) {
                                        layer.closeAll('index');
                                        layer.alert(data.msg,function(){
                                            sessionStorage.removeItem("accessToken");
                                            sessionStorage.clear();
                                            localStorage.clear();
                                            location.href = "./login.html";
                                        });
                                        
                                    }else{
                                        layer.msg(data.msg,{icon:2,time:2000});
                                    }
                                }
                            })
                            
                        })
                    }
                    ,success: function(layero) {
                        // 添加form标识
                        layero.addClass('layui-form');
                        // 将保存按钮改变成提交按钮
                        layero.find('.layui-layer-btn0').attr({
                            'lay-filter': 'Editpassword',
                            'lay-submit': ''
                    });}
                  });    
            },
            backLogin:function(){
                backLogin()
            }
        }
        $('.bao_loginbtn').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        }); 
        // 返回登陆页时的操作
        var backLogin=function(){
            layer.alert("确定要退出登录吗？",function(){
                $.ajax({
                    url: "gt/opm/login/logout", //请求地址
                    type: "PUT", //提交的方式S
                    data:{"id":sessionStorage.getItem("userid")}, //提交的数据
                    dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                    beforeSend: function() {
                        loading=layer.load(2);
                    },
                    ContentType:"application/json;charset=UTF-8",
                    success: function(data) { 
                        layer.closeAll('loading');
                        if (data.flag == true) {
                            layer.closeAll('index');
                            sessionStorage.removeItem("accessToken");
                            sessionStorage.clear();
                            localStorage.clear();
                            location.href = "./login.html";
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    }
                })
            })
        }
       var html='<form  class="layui-form" action="" lay-filter="Editpassword">'
            +'<div style="padding:30px 30px 0" class="bao_alert">'
                +'<lable>旧密码</lable>'
                +'<div class="layui-input-block" style="margin:14px 0;">'
                +'<input type="text" name="oldpass" lay-verify="required" placeholder="旧密码" autocomplete="off" class="layui-input">'
                +'</div>'
                +'<lable>新密码</lable>'
                +'<div class="layui-input-block" style="margin:14px 0;">'
                +'<input type="password" maxlength="90" name="newpass" lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input">'
                +'</div>'
                +'<lable>确认密码</lable>'
                +'<div class="layui-input-block" style="margin:14px 0;">'
                +'<input type="password" maxlength="90" name="secondpass" lay-verify="required" placeholder="请再次输入新密码" autocomplete="off" class="layui-input">'
                +'</div>'
                +'<p class="oneBorder"></p>'
            +'</div>' 
        +'</form>' 
      
});
